package cn.ltgodm.sandbox.controller;

import cn.ltgodm.sandbox.model.dto.ExecuteCodeRequest;
import cn.ltgodm.sandbox.model.dto.ExecuteCodeResponse;
import cn.ltgodm.sandbox.service.CodeSandboxService;
import cn.ltgodm.sandbox.service.CodeSandboxServiceFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author ltgodm
 * @date 2024-03-10 18:58:05
 */
@RestController
@Slf4j
@RequestMapping("/sandbox")
public class SandboxController {
    @Resource
    private CodeSandboxServiceFactory codeSandboxServiceFactory;

    @PostMapping("execute")
    public ExecuteCodeResponse executeCode(@RequestBody ExecuteCodeRequest executeCodeRequest){
        String language = executeCodeRequest.getLanguage();
        CodeSandboxService codeSandboxService = codeSandboxServiceFactory.getCodeSandboxByLanguage(language);
        ExecuteCodeResponse executeCodeResponse = codeSandboxService.executeCode(executeCodeRequest);
        log.info("返回结果：{}",executeCodeResponse);
        return executeCodeResponse;
    }
}
